package com.czxy.service;

import com.czxy.dao.ProductMapper;
import com.czxy.domain.Product;
import com.czxy.domain.ProductVo;
import com.czxy.utils.MyBatisUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

public class ProductService {


    // 查询
    public PageInfo findProductByVo(ProductVo vo,int pageNum,int pageSize) {


        //获取mapper
        ProductMapper mapper = MyBatisUtils.getMapper(ProductMapper.class);
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //拼装条件
        Example example = new Example(Product.class);
        Example.Criteria ct = example.createCriteria();

        //分类id
        if (vo.getCid()!=null && !"".equals(vo.getCid())){
            ct.andEqualTo("cid",vo.getCid());
        }

        //价格 区间
        if (vo.getLowPrice() !=null && vo.getLowPrice() !=0){

            ct.andGreaterThanOrEqualTo("shopPrice",vo.getLowPrice());
        }

        if (vo.getHighPrice() !=null && vo.getHighPrice() !=0){

            ct.andGreaterThanOrEqualTo("shopPrice",vo.getLowPrice());

        }

        //查询
        List<Product> products = mapper.selectByExample(example);


        //封装结果 并返回
        PageInfo<Product> pageInfo = new PageInfo<>(products);

        //释放资源
        MyBatisUtils.close();
        return pageInfo;


    }
}
